<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="online-genDataTableColumn" setup>
import { reactive, onMounted, } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { columns, searchFormSchema } from './GenDataTableColumnData';
import { list, deleteOne, getImportUrl, getExportUrl } from './GenDataTableColumnApi';
import { useMessage } from '/@/hooks/web/useMessage'; // 轻提醒
const { createMessage: $message } = useMessage();
const queryParam = reactive<any>({});
import { useRoute } from 'vue-router';

//注册table数据
const { tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '系统数据库字段表',
    api: list,
    columns,
    canResize: false,
    showIndexColumn: true,
    showActionColumn: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "系统数据库字段表",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { getForm, reload }, { selectedRowKeys }] = tableContext

const route = useRoute();
onMounted(async () => {
  //接收路由参数：待进一步测试
  let tableName = route.query?.tableName;
  if (tableName) {
    getForm().setFieldsValue({
      tableName: tableName,
    });
  }
  // $message.info(JSON.stringify(route));
})

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id });
  handleSuccess();
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 表格操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '删除',
      icon: 'ant-design:delete-outlined',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        placement: 'left',
        confirm: handleDelete.bind(null, record),
      }
    }
  ]
}
</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}
</style>